package com.daigou.admin.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daigou.constants.CommonConstants;
import com.daigou.dto.AjaxResult;
import com.daigou.model.Shop;
import com.daigou.model.User;
import com.daigou.service.ShopService;

@Controller("adminShopController")
@RequestMapping(value = "/admin")
public class ShopController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShopController.class);

    @Autowired
	private ShopService shopService;
    
    @RequestMapping(value = "/shop-info",method = RequestMethod.GET)
    public String toShopInfoPage(HttpServletRequest request,Model model){
		LOGGER.info("进入我的店铺页面");
		User user = (User)request.getAttribute(CommonConstants.USER_INFO);
		model.addAttribute("user", user);
		Shop shop = (Shop)request.getAttribute(CommonConstants.SHOP_INFO);
		model.addAttribute("shop", shop);
        return "/admin/app/shop_info";
    }
    @RequestMapping(value = "/shop-update",method = RequestMethod.GET)
    public String toShopUpdatePage(HttpServletRequest request,Model model){
		LOGGER.info("进入我的店铺页面");
		User user = (User)request.getAttribute(CommonConstants.USER_INFO);
		Shop shop = (Shop)request.getAttribute(CommonConstants.SHOP_INFO);
		model.addAttribute("user", user);
		model.addAttribute("shop", shop);
        return "/admin/app/shop_update";
    }
    @RequestMapping(value = "/shop-update",method = RequestMethod.POST)
	@ResponseBody
    public AjaxResult updateDoneShop(HttpServletRequest request,@RequestBody Shop shop){
		LOGGER.info("进入完善商铺信息页面");
		try{
			shopService.updateByPrimaryKeySelective(shop);
			return new AjaxResult(true,"店铺信息更新成功!");
		}catch(Exception ex){
			return new AjaxResult(false,"店铺信息更新失败,请刷新后重试或联系管理员!");
		}
    }
}
